<?php
class PhotosController extends AppController
{
     var $components = array('PImage');
    
    
    
    public function resize()
    {
        if($this->PImage->resizeImage('resize', $this->data['Photo']['file'].'.'.$this->data['Photo']['ext'], 'img/'.$this->Auth->user('id').'/', false, 750, false));
    }
    
    public function crop()
    {
       if($this->PImage->resizeImage('crop', $this->data['Photo']['file'].'.'.$this->data['Photo']['ext'], 'img/'.$this->Auth->user('id').'/', $this->data['Photo']['file'].'_cropped.'.$this->data['Photo']['ext'], 70, 70));
    }
    
    public function index()
    {
        
    }
    
    function add($data, $moment = null) 
    {
        if (!empty($data) &&
             is_uploaded_file($data['Photo']['tmp_name'])) {

            $this->data['Photo']['name'] = $data['Photo']['name'];
            switch($data['Photo']['type'])
            {
                case 'image/gif':
                    $this->data['Photo']['ext'] = 'gif';
                    break;
                case 'image/jpeg':
                    $this->data['Photo']['ext'] = 'jpg';
                    break;
                case'image/pjpeg':
                    $this->data['Photo']['ext'] = 'fjpg';
                    break;
                case 'image/png':
                    $this->data['Photo']['ext'] = 'png';
                    break;
            }
            
            if(!is_dir('img/'.$this->Auth->user('id')))
            {
                mkdir('img/'.$this->Auth->user('id'));
            }
            
            $this->data['Photo']['file'] = $moment;
            
            $success = move_uploaded_file($data['Photo']['tmp_name'], 'img/'.$this->Auth->user('id').'/'.$moment.'.'.$this->data['Photo']['ext']);
            
            $this->data['Photo']['size'] = $data['Photo']['size'];
            $this->data['Photo']['moment_id'] = $moment;
            
            
            $this->Photo->save($this->data);
            $this->resize();
            $this->crop();
            
           

            return $success;
        }
        
        
        
        return 0;
    }
}

?>
